{% extends 'index.html' %} 
{% block ace-content %} 
<!-- Search -->   
<link rel="stylesheet" href="/static/dist/css/jquery-ui.min.css">
<script src="/static/dist/js/jquery-ui.min.js"></script> 
{% endblock %}
{%block page-content %}
<div id="page-wrapper">

	<div class="row">
		<div class="col-lg-12">
			<h1 class="page-header">
				<i class="fa  fa-list-alt"></i>&nbsp;
				<code>历史工单查询</code>
			</h1>
		</div>
		<!-- /.col-lg-12 -->
	</div>
	<div class="row">
		<div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                            	<i class="fa fa-search-plus"></i> 查询类型 
                </div>
                        
                <div class="panel-body">
				


				   <div class="row" >
				      <div class="col-xs-6 col-sm-3">
							<div class="form-group">
								<label class="col-sm-3 control-label">申请人:</label>
								<div class="col-sm-8">
									 <select class="form-control" id="order_user" >
									 	<option selected="selected" value="">请选择一个名称</option>
									 	{% for ds in userList %}
									 		<option value="{{ds.id}}" name="order_user">{{ds.username}}</option>
									 	{% endfor %}			
									</select>
								</div>
							</div>
				      </div>
				      <div class="col-xs-6 col-sm-3">
							<div class="form-group">
								<label class="col-sm-3 control-label">工单状态:</label>
								<div class="col-sm-8">
									 <select class="form-control" id="order_status" >
									 	<option selected="selected" value="">请选择一个类型</option>
									 	<option value="1" name="order_status">已拒绝</option>		
									 	<option value="2" name="order_status">审核中</option>		
									 	<option value="3" name="order_status">已部署</option>	
									 	<option value="4" name="order_status">待授权</option>	
									 	<option value="5" name="order_status">已执行</option>	
									 	<option value="6" name="order_status">已回滚</option>	
									 	<option value="7" name="order_status">已撤回</option>	
									 	<option value="8" name="order_status">已授权</option>	
									 	<option value="8" name="order_status">已失败</option>	
									</select>
								</div>
							</div>
				      </div>
				
				      <div class="col-xs-6 col-sm-3">
							<div class="form-group">
								<label class="col-sm-3 control-label">工单执行人:</label>
								<div class="col-sm-8">
									 <select class="form-control" id="order_executor" >
									 	<option selected="selected" value="">请选择一个类型</option>
									 	{% for ds in userList %}
									 		<option value="{{ds.id}}" name="order_executor">{{ds.username}}</option>
									 	{% endfor %}																
									</select>
								</div>
							</div>
				      </div>
				

				      <div class="col-xs-6 col-sm-3">
							<div class="form-group">
								<label class="col-sm-3 control-label">工单类型:</label>
								<div class="col-sm-8">
									 <select class="form-control" id="order_type" >
									 	<option selected="selected" value="">请选择一个类型</option>
                                			<option value="0" name="order_type">SQL更新</option>                    				
                                			<option value="1" name="order_type">代码部署</option>		
                                			<option value="2" name="order_type">文件分发</option>		
                                			<option value="3" name="order_type">文件下载</option>													
									</select>
								</div>
							</div>
				      </div>				      
				      	   
				   </div>   			                                                                
		                                                                      
                        
                    </div>
                </div>        
            </div>	
	</div>
	
	<div class="row">
		<div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                            <i class="fa fa-check "></i> 已选条件
                </div>
                        
                <div class="panel-body">
                        <div id="divSelectedType">
                        </div>
                </div>
            </div>
         </div> 	
	</div>
	
	<div class="row">

	
	
		<div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                           <i class="fa fa-tasks "></i> 结果列表
                </div>
                        
                <div class="panel-body">
                	<div id="div-search-result" style="display:none;">
                		<div class="col-lg-10">
                		</div>                       
                        <div>
							<table   width="100%" class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-Search">						
	                                <thead>
		                                    <tr>
		                                    	<th class="text-center">ID</th>
		                                    	<th class="text-center">工单类型</th>
		                                    	<th class="text-center">工单申请人</th>
		                                    	<th class="text-center">工单主题</th>
		                                    	<th class="text-center">工单内容</th>
		                                        <th class="text-center">执行人</th>
		                                        <th class="text-center">发布时间</th>
		                                        <th class="text-center">工单状态</th>
		                                        <th class="text-center">操作</th>
		                                    </tr>
                                    <tfoot>
				                         <tr>
	                                    	<th class="text-center">ID</th>
	                                    	<th class="text-center">工单类型</th>
	                                    	<th class="text-center">工单申请人</th>
	                                    	<th class="text-center">工单主题</th>
	                                    	<th class="text-center">工单内容</th>
	                                        <th class="text-center">执行人</th>
	                                        <th class="text-center">发布时间</th>
	                                        <th class="text-center">工单状态</th>
	                                        <th class="text-center">操作</th>
	                                    </tr>	
                                    </tfoot>									    
	                                </thead>
	                       	</table>
                       	</div>
                   </div>                   
                </div>
            </div>
         </div>	
	
</div>



</div>

<script type="text/javascript">

	$('#order_user').change(function () {
	    if ($('#order_user').val() != "") {
	        $("#hdnorderapply").val($('#order_user').val());
	        var span = "<span class='label label-success' id='orderapply'>" + $("#order_user").find("option:selected").text()
	        + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_user' type='hidden' value='"
	        + $('#order_user').val() + "' /></span> &nbsp;";
	        if ($("#orderapply").length == 0) {
	            $('#divSelectedType').append(span);
	        }
	        else {
	            $("#orderapply").html($("#order_user").find("option:selected").text()
	             + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_user' type='hidden' value='"
	             + $('#order_user').val() + "' /></span> &nbsp;");
	        }
	        changepage(1);
	    }
	});
	
	$('#order_executor').change(function () {
	    if ($('#order_executor').val() != "") {
	        $("#hdnorderexecutor").val($('#order_executor').val());
	        var span = "<span class='label label-success' id='orderexecutor'>" + $("#order_executor").find("option:selected").text()
	        + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_executor' type='hidden' value='"
	        + $('#order_executor').val() + "' /></span> &nbsp;";
	        if ($("#orderexecutor").length == 0) {
	            $('#divSelectedType').append(span);
	        }
	        else {
	            $("#orderexecutor").html($("#order_executor").find("option:selected").text()
	             + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_executor' type='hidden' value='"
	             + $('#order_executor').val() + "' /></span> &nbsp;");
	        }
	        changepage(1);
	    }
	});


     
	$('#order_status').change(function () {
	    if ($('#order_status').val() != "") {
	        $("#hdnorderstatus").val($('#order_status').val());
	        var span = "<span class='label label-success' id='orderstatus'>" + $("#order_status").find("option:selected").text()
	        + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_status' type='hidden' value='"
	        + $('#order_status').val() + "' /></span> &nbsp;";
	        if ($("#orderstatus").length == 0) {
	            $('#divSelectedType').append(span);
	        }
	        else {
	            $("#orderstatus").html($("#order_status").find("option:selected").text()
	             + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_status' type='hidden' value='"
	             + $('#order_status').val() + "' /></span> &nbsp;");
	        }
	        changepage(1);
	    }
	});	

	$('#order_type').change(function () {
	    if ($('#order_type').val() != "") {
	        $("#hdnorderdb").val($('#order_type').val());
	        var span = "<span class='label label-success' id='orderdb'>" + $("#order_type").find("option:selected").text()
	        + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_type' type='hidden' value='"
	        + $('#order_type').val() + "' /></span> &nbsp;";
	        if ($("#orderdb").length == 0) {
	            $('#divSelectedType').append(span);
	        }
	        else {
	            $("#orderdb").html($("#order_type").find("option:selected").text()
	             + " <img alt='' src='/static/img/close.png' onclick='removeself(this)' /><input name='order_type' type='hidden' value='"
	             + $('#order_type').val() + "' /></span> &nbsp;");
	        }
	        changepage(1);
	    }
	});

    function search_go() {
        var parameter = {};
        $("input[type='hidden']").each(function () {
            var key = $(this).prop('name');
            var value = $(this).val();
            parameter[key] = value;
        })

        var count = 0;
        for (var i in parameter) {
            count += i;
            break;
        }
        if (count == 0) {
            return false;
        }

        $.post('/order/search/', parameter, function (result) {
            if (result["data"].length > 0) {
            	document.getElementById("div-search-result").style.display = "";
					 var table = $('#dataTables-Search').dataTable();
					 oSettings = table.fnSettings();
					 table.fnClearTable(this);
					 for (var i=0; i<result["data"].length; i++)
					 {
					   table.oApi._fnAddData(oSettings, result["data"][i]);
					 }
					 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
					 table.fnDraw();                	               	
            }
            else{
            	//没有数据就清空
            	var table = $('#dataTables-Search').dataTable();
            	table.fnClearTable(this);
            }
        })
    }

    function changepage(pageindex) {
        curpage = pageindex;
        search_go();
    }

    function removeself(obj) {
        $(obj).parent().remove();
        changepage(1);
    }	

	$(document).ready(function() {
	    $('#taskTableList').DataTable();
	} ); 

	function deleteSqlOrder(obj,id){
		var txt=  "是否确认删除？";
		var btnObj = $(obj);
		btnObj.attr('disabled',true);
		var option = {
			title: "工单"+id,
			btn: parseInt("0011",2),
			onOk: function(){
				$.ajax({
					  type: 'DELETE',
					  url: '/api/orders/'+id+'/',
				      success:function(response){	
				    	btnObj.removeAttr('disabled');
	                	window.wxc.xcConfirm("删除成功！", window.wxc.xcConfirm.typeEnum.success);
	                	location.reload();				            
				      },
		              error:function(response){
		            	btnObj.removeAttr('disabled');
		            	window.wxc.xcConfirm("删除失败！", window.wxc.xcConfirm.typeEnum.error);	
		              }
					});
			},
			onCancel:function(){	
			},
			onClose:function(){
			}
		}
		window.wxc.xcConfirm(txt, "custom", option);			   
	}    
	
	function updateSqlOrderStatus(obj,id,model){ 
		if (model == 'auth' ){
			if (model == 'auth'){
				var txt=  "是否确认授权？";
				var title = "SQL工单授权";
			}
			var option = {
				title: title,
				btn: parseInt("0011",2),
				onOk: function(){
					$.ajax({
						  type: 'PUT',
						  url: '/api/orders/'+id+'/',
						  data: {
							  "order_status":8
						  },
					      success:function(response){			            
				            window.wxc.xcConfirm("授权成功", window.wxc.xcConfirm.typeEnum.success);
				            location.reload();
		                
						},
			            error:function(response){
			            	window.wxc.xcConfirm("授权失败！", window.wxc.xcConfirm.typeEnum.error);
			            },							  
						});
				},
				onCancel:function(){	
				},
				onClose:function(){
				}
			}
			window.wxc.xcConfirm(txt, "custom", option);				
		} 		
		else if (model == 'disable'){
			window.wxc.xcConfirm("撤销原因：", window.wxc.xcConfirm.typeEnum.input,{
				onOk:function(result){
					if (result.length == 0){
						/* 如果没有输入字符串则直接退出 */
						return;
					};
					$.ajax({
						  type: 'PUT',
						  url: '/api/orders/'+id+'/',
						  data: {
							  'order_cancel':result,
							  "order_status":7
						  },
					     success:function(response){	
				            window.wxc.xcConfirm("撤销成功", window.wxc.xcConfirm.typeEnum.success);
				            location.reload();
						},
			            error:function(response){
			            	btnObj.removeAttr('disabled');
			            	window.wxc.xcConfirm("服务器响应错误，撤销失败", window.wxc.xcConfirm.typeEnum.error);
			            },							  
						});
					
				}
			})			
		}
   }	
	
</script>

{% endblock %}
